package com.hacpai.android.md.presenter.implement;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.hacpai.android.md.R;
import com.hacpai.android.md.model.api.ApiClient;
import com.hacpai.android.md.model.api.ApiDefine;
import com.hacpai.android.md.model.api.DefaultCallback;
import com.hacpai.android.md.model.api.ForegroundCallback;
import com.hacpai.android.md.model.entity.Result;
import com.hacpai.android.md.model.storage.LoginShared;
import com.hacpai.android.md.presenter.contract.ILoginPresenter;
import com.hacpai.android.md.ui.view.ILoginView;
import com.hacpai.android.md.util.Digest;
import com.hacpai.android.md.util.FormatUtils;

import okhttp3.Headers;
import retrofit2.Call;

public class LoginPresenter implements ILoginPresenter {

    private final Activity activity;
    private final ILoginView loginView;

    public LoginPresenter(@NonNull Activity activity, @NonNull ILoginView loginView) {
        this.activity = activity;
        this.loginView = loginView;
    }

    @Override
    public void loadUserAsyncTask(String userName) {
        if (!TextUtils.isEmpty(userName)) {
            ApiClient.service.getUser(userName).enqueue(new ForegroundCallback<Result.Data<Result.UserDate>>(activity) {

                @Override
                public boolean onResultOk(int code, Headers headers, Result.Data<Result.UserDate> result) {
                    Log.d("xjf1128", "UserOk:"+result.getData().getUser().getUserNickname());
                        LoginShared.update(getActivity(), result.getData().getUser());
                    loginView.onLoginOk();
                    return false;
                }

            });
        }
    }

    //hac login
    @Override
    public void loginAsyncTask(final String loginName,final String userPassword, String captcha) {
        if (captcha==null) {
           captcha="";
        } else {
            String md5Password = Digest.MD5.getHex(userPassword);
            Call<Result.LoginDate> call = ApiClient.service.login(new ApiDefine.LoginUser(loginName,md5Password,captcha));
            loginView.onLoginStart(call);
            call.enqueue(new DefaultCallback<Result.LoginDate>(activity) {

                @Override
                public boolean onResultOk(int code, Headers headers, Result.LoginDate loginInfo) {
                    if(loginInfo.getToken()==null||loginInfo.getToken().equals("")){
                        loginView.onLoginError(loginInfo.getMsg(),loginInfo.getNeedCaptcha());
                    }
                    else{
                        LoginShared.login(getActivity(),loginInfo.getUserName(),userPassword,loginInfo.getToken());
                        loadUserAsyncTask(loginInfo.getUserName());
                        Log.d("xjf1128","onResultOk: "+loginInfo.getToken());
                    }
                    return false;
                }

                @Override
                public boolean onResultAuthError(int code, Headers headers, Result.Error error) {
                    loginView.onAccessTokenError(getActivity().getString(R.string.access_token_auth_error));
                    Log.d("xjf1128", "onResultAuthError");
                    return false;
                }

                @Override
                public void onFinish() {
                    loginView.onLoginFinish();
                }

            });
        }

    }

    @Override
    public void loginAsyncTask(final String accessToken) {
        if (!FormatUtils.isAccessToken(accessToken)) {
            loginView.onAccessTokenError(activity.getString(R.string.access_token_format_error));
        } else {
            Call<Result.Login> call = ApiClient.service.accessToken(accessToken);
            //loginView.onLoginStart(call);    待修改接口
            call.enqueue(new DefaultCallback<Result.Login>(activity) {

                @Override
                public boolean onResultOk(int code, Headers headers, Result.Login loginInfo) {
                    loginView.onAccessTokenOk(accessToken, loginInfo);
                    return false;
                }

                @Override
                public boolean onResultAuthError(int code, Headers headers, Result.Error error) {
                    loginView.onAccessTokenError(getActivity().getString(R.string.access_token_auth_error));
                    return false;
                }

                @Override
                public void onFinish() {
                    loginView.onLoginFinish();
                }

            });
        }
    }

}
